﻿using System;
using System.Globalization;
namespace YanZhiwei.DotNet2.Utilities.Common
{
    /// <summary>
    /// 十六进制帮助类
    /// </summary>
    public class HexHelper
    {
        static decimal dHexValue = 255;
        #region 将百分比转换成十六进制字符串
        /// <summary>
        /// 将百分比转换成十六进制字符串
        /// </summary>
        /// <param name="number">80即表示80%</param>
        /// <param name="capacity">不足设定的位数的时候，向左边补充'0'</param>
        /// <returns>十六进制字符串</returns>
        public static string PercentToHex(decimal number, int capacity)
        {
            double _percent = ((double)number) / 100;
            double _number = Math.Round(_percent * 255, 0);
            return string.Format("{0:x}", (int)_number).PadLeft(capacity, '0');
        }
        #endregion
        #region 将十六进制转换成百分比
        /// <summary>
        /// 将十六进制转换成百分比
        /// </summary>
        /// <param name="hex">需要转换的十六进制</param>
        /// <returns>百分比</returns>
        public static decimal HexToPercent(string hex)
        {
            decimal _result = -1;
            int _percent = -1;
            if (int.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out _percent))
                _result = Math.Round((_percent / dHexValue) * 100, 2);
            else
                throw new Exception("PercentToHex Exception:十六进制值转换失败。");
            return _result;
        }
        #endregion
        #region 将十六进制字符串转换成十进制 eg:FF=>255
        /// <summary>
        /// 将十六进制字符串转换成十进制
        /// <para> eg:FF=>255</para>
        /// </summary>
        /// <param name="hexString">十六进制字符串</param>
        /// <returns>Decimal</returns>
        public static decimal ToDecimal(string hexString)
        {
            return Convert.ToInt64(hexString, 16);
        }
        #endregion 
        #region 将十六进制字符串转换INT
        /// <summary>
        /// 将十六进制字符串转换INT
        /// </summary>
        /// <param name="hexString">十六进制字符串</param>
        /// <returns>INT</returns>
        public static int ToInt(string hexString)
        {
            return int.Parse(hexString, NumberStyles.AllowHexSpecifier);
        }
        #endregion 
        #region  将INT转换成十六进制字符串
        /// <summary>
        /// 将INT转换成十六进制字符串
        /// </summary>
        /// <param name="number">int</param>
        /// <param name="capacity">长度，不足则向左不足0</param>
        /// <returns>十六进制字符串</returns>
        public static string ToHexString(int number, int capacity)
        {
            return ToHexString(number).PadLeft(capacity, '0');
        }
        /// <summary>
        ///将INT转换成十六进制字符串
        /// </summary>
        /// <param name="number">int</param>
        /// <returns>十六进制字符串</returns>
        public static string ToHexString(int number)
        {
            string _hex = string.Format("{0:X}", number);
            if (_hex.Length % 2 != 0)
                _hex = string.Format("0{0}", _hex);
            return _hex;
        }
        #endregion
    }
}